<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('reportModule.conversionData.data')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('collectionManagement.report.data.reBorrow')">
          <el-select v-model="searchForm.firstLoan" clearable :placeholder="$t('collectionManagement.report.prompt.select')">
            <el-option
              v-for="item in $t('collectionManagement.report.borrowData')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('reportModule.conversionData.deviceType')">
          <el-select v-model="searchForm.deviceType" clearable :placeholder="$t('common.select')">
            <el-option
              v-for="item in $t('reportModule.conversionData.deviceData')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
        <el-row style="margin-top: 20px">
          <el-button :type="dayType === 1 ? 'primary' : ''" @click="setDay">
            {{ $t('reportModule.conversionData.sevenDay') }}
          </el-button>
          <el-button :type="dayType === 2 ? 'primary' : ''" @click="setMonth">
            {{ $t('reportModule.conversionData.month') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-row>
        <h3>{{ $t('reportModule.conversionData.total') }}</h3>
        <el-divider />
        <el-table
          v-loading="totalLoading"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :data="tableDataTotal"
          border
          :stripe="true"
          style="width: 100%"
        >
          <el-table-column
            v-for="(item, index) in columns"
            :key="index"
            :prop="item.prop"
            :label="tableText[item.label]"
            min-width="150"
          >
            <template slot-scope="scope">
              {{ scope.row[item.prop] || 0 }}
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-card>
    <el-card>
      <el-row>
        <h3>{{ $t('reportModule.conversionData.detail') }}</h3>
        <el-divider />
        <el-table
          v-loading="loading"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :data="tableData"
          border
          :stripe="true"
          style="width: 100%"
        >
          <el-table-column
            prop="dateStr"
            :label="tableText['date']"
            min-width="120"
          />
          <el-table-column
            v-for="(item, index) in columns"
            :key="index"
            :prop="item.prop"
            :label="tableText[item.label]"
            min-width="150"
          >
            <template slot-scope="scope">
              {{ scope.row[item.prop] || 0 }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            :current-page="searchForm.currentPage"
            :page-sizes="[10, 20, 30, 40, 50]"
            :page-size="searchForm.pageSize"
            :total="totalNumber"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-row>
    </el-card>
  </el-row>
</template>

<script>
import { platformOperate, platformOperateDetail, platformOperateDetailExport } from '@/api/report'
import { formatDate } from '@/utils/method'
export default {
  name: 'PlatformOperationData',
  data() {
    return {
      loading: false,
      totalLoading: false,
      tableData: [],
      tableDataTotal: [],
      tableText: {},
      totalNumber: 0,
      time: [new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 6), new Date()],
      dayType: 1,
      searchForm: this.params(),
      columns: [
        {
          label: 'downloads',
          prop: 'downloadUser'
        },
        {
          label: 'softwareStartup',
          prop: 'appStart'
        },
        {
          label: 'registeredUser',
          prop: 'register'
        },
        {
          label: 'login',
          prop: 'login'
        },
        {
          label: 'loanApplicationOrders',
          prop: 'applyLoan'
        },
        {
          label: 'successfulLoanOrders',
          prop: 'paymentSuccess'
        },
        {
          label: 'totalAmount',
          prop: 'paymentAmount'
        },
        {
          label: 'rollovers',
          prop: 'extension'
        },
        {
          label: 'repaymentTotal',
          prop: 'repaymentAmount'
        },
        {
          label: 'advanceRepayAmount',
          prop: 'advanceRepayAmount'
        },
        {
          label: 'currentDayRepayAmount',
          prop: 'currentDayRepayAmount'
        },
        {
          label: 'dueRepayAmount',
          prop: 'dueRepayAmount'
        },
        {
          label: 'doneOrderCount',
          prop: 'doneOrderCount'
        },
        {
          label: 'beforeDoneOrderCount',
          prop: 'beforeDoneOrderCount'
        },
        {
          label: 'currentDayDoneOrderCount',
          prop: 'currentDayDoneOrderCount'
        },
        {
          label: 'dueDoneOrderCount',
          prop: 'dueDoneOrderCount'
        },
        {
          label: 'currentDayRepayOrderCount',
          prop: 'currentDayRepayOrderCount'
        },
        {
          label: 'dueDateRepayCount',
          prop: 'dueDateRepayCount'
        },
        {
          label: 'dueUndoneOrderCount',
          prop: 'dueUndoneOrderCount'
        },
        {
          label: 'firstDueRate',
          prop: 'firstDueRate'
        },
        {
          label: 'currentDayOutghtRepayAmount',
          prop: 'currentDayOutghtRepayAmount'
        },
        {
          label: 'dueDateRepayAmount',
          prop: 'dueDateRepayAmount'
        },
        {
          label: 'dueUndoneOrderAmount',
          prop: 'dueUndoneOrderAmount'
        },
        {
          label: 'amountFirstDueRate',
          prop: 'amountFirstDueRate'
        }
      ]
    }
  },
  mounted() {
    this.tableText = this.$t('reportModule.operationData.table')
    console.log(this.tableText['dueDateRepayAmount'])
    this.getData()
    this.getTotalData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
        firstLoan: '',
        deviceType: ''
      }
    },
    setDay() {
      this.dayType = 1
      this.time = [new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 6), new Date()]
      this.getData()
      this.getTotalData()
    },
    setMonth() {
      this.dayType = 2
      this.time = [new Date((new Date()).getTime() - 1000 * 60 * 60 * 24 * 30), new Date()]
      this.getData()
      this.getTotalData()
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
      this.getTotalData()
    },
    reset() {
      this.searchForm = this.params()
      this.setDay()
      this.getData()
      this.getTotalData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['startTime'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['endTime'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['startTime'] = null
        data['endTime'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      platformOperateDetail(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    getTotalData() {
      this.totalLoading = true
      platformOperate(this.searchData())
        .then(res => {
          this.totalLoading = false
          if (res.success) {
            this.tableDataTotal = [res.data]
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.totalLoading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      platformOperateDetailExport(this.searchData())
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
